#ifndef __COMMON_H__
#define __COMMON_H__

#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "cJSON.h"

#include "cc.h"
#include "log.h"
#include "config.h"

#define DEBUG

#define CMD_LENGTH      20
 
#define COMMON_ERROR_BASE 1000

#define MAX_LOGINS 5

#define END_MARK "#ds#"
#define END_LENGTH 4

#define COM_TX     0
#define COM_RX     1

#define main_firmware_version   "0.0.3"

enum common_error{
    NO_ERROR = 0,
    UNKNOWN_ID = COMMON_ERROR_BASE + 1,
};

enum print_id{
    print_oxc,
    print_wss,
    print_opm,
    print_upper,
    print_alarm,
    print_debug
};

typedef enum _CMD_ID{
    id_user_login = 1,
    id_user_loginout = 2,
    id_user_changepwd = 3,

    id_update = 4,

    id_alarm_report = 11,
    id_alarm_check = 12,

    id_opm_scan = 21,
    id_opm_ver = 22,
    id_opm_reset = 23,

    id_wss_set_custom = 41,
    id_wss_get_custom = 42,
    id_wss_reset = 43,
    id_wss_get_temp = 44,
    id_wss_get_info = 45,
    id_wss_set_channel_spacing = 46,
    id_wss_get_channel_spacing = 47,
    id_wss_set_custom_channel = 48,
    id_wss_get_custom_channel = 49,


    id_oxc_set_custom = 61,
    id_oxc_get_custom = 62,
    id_oxc_reset = 63,
    id_oxc_get_info = 64,
    id_oxc_get_power = 65,
    id_oxc_set_atten = 66,
    id_oxc_get_atten = 67,

    id_save_default = 81,
    id_load_default = 82,
    id_protocol_exception = 200,
}CMD_ID;

typedef struct _error_code{
    int id;
    char* message;
}error_code;


#define OFFSET(type,m) (size_t)&(((type*)0)->m)
#define SIZE(type,m) (size_t)(sizeof(((type*)0)->m))

char* return_error_message(int id);
int convert_string2hex(unsigned char *in_data, int in_data_len, unsigned char *out_data, int *out_data_len);
int convert_hex2string(unsigned char *in_data, int in_data_len, unsigned char *out_data, int *out_data_len,int bit);

int find_key(char* pattern,char* source,int* begin,int* end,int is_array,int index);
int find_keys(char* pattern,char* source,size_t num,...);
char* time_to_string(uint32_t time,char* s_time,char* unit);

char* strmove(char* dst,char* src,int len);
void htonls(int* data,int n);

void set_printf_flag(int flag,int value);
void reset_printf_flag();
void printf_flag(int flag,char* fmt,...);
void printf_comm_flag(int dir,int flag,char* prefix,char* data,int len);

void update_date(char* date);
void get_date(char* date);
void get_time(char* time);
void get_exact_time(char* time);
void array_to_string(void* array,int size,int bit,char* out,int type);
void string_to_array(char* s_list,void* array,int* size,int type);
#endif // !__COMMON_H__
